package com.ruoyi.store.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.store.service.IStockInDetailService;
import com.ruoyi.system.api.domain.store.StockInDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 入库记录详情Controller
 *
 * @author lmj
 * @date 2024-10-31
 */
@RestController
@RequestMapping("/stockInDetail")
@Slf4j
public class StockInDetailController extends BaseController {
    @Autowired
    private IStockInDetailService stockInDetailService;

    /**
     * 查询入库记录详情列表
     */
    @RequiresPermissions("store:stockInDetail:list")
    @GetMapping("/list")
    public TableDataInfo list(StockInDetail stockInDetail) {
        startPage();
        List<StockInDetail> list = stockInDetailService.selectStockInDetailList(stockInDetail);
        return getDataTable(list);
    }

    @RequiresPermissions("store:stockInDetail:list")
    @PostMapping("/getStockInDetail")
    public List<StockInDetail> getStockInDetail(@RequestBody Integer[] idArray) {
        log.info("getStockInDetail idArray:" + idArray);
        List<StockInDetail> stockInDetails = stockInDetailService.getStockInDetail(idArray);
        return stockInDetails;
    }

    //获取能够退货数量
    @GetMapping("/getStockInDetailListByStockInId")
    public R<List<StockInDetail>> getStockInDetailListByStockInId(@RequestParam("stockInId") Integer stockInId) {
        log.info("getStockInDetailListByStockInId stockInId:" + stockInId);
        List<StockInDetail> stockInDetailList = stockInDetailService.getStockInDetailListByStockInId(stockInId);
        log.info("getStockInDetailListByStockInId stockInDetailList:" + stockInDetailList);
        return R.ok(stockInDetailList);
    }

    /**
     * 导出入库记录详情列表
     */
    @RequiresPermissions("store:stockInDetail:export")
    @Log(title = "入库记录详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockInDetail stockInDetail) {
        List<StockInDetail> list = stockInDetailService.selectStockInDetailList(stockInDetail);
        ExcelUtil<StockInDetail> util = new ExcelUtil<StockInDetail>(StockInDetail.class);
        util.exportExcel(response, list, "入库记录详情数据");
    }

    /**
     * 获取入库记录详情详细信息
     */
    @RequiresPermissions("store:stockInDetail:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(stockInDetailService.selectStockInDetailById(id));
    }

    /**
     * 新增入库记录详情
     */
    @RequiresPermissions("store:stockInDetail:add")
    @Log(title = "入库记录详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockInDetail stockInDetail) {
        return toAjax(stockInDetailService.insertStockInDetail(stockInDetail));
    }

    /**
     * 修改入库记录详情
     */
    @RequiresPermissions("store:stockInDetail:edit")
    @Log(title = "入库记录详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockInDetail stockInDetail) {
        return toAjax(stockInDetailService.updateStockInDetail(stockInDetail));
    }

    /**
     * 删除入库记录详情
     */
    @RequiresPermissions("store:stockInDetail:remove")
    @Log(title = "入库记录详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(stockInDetailService.deleteStockInDetailByIds(ids));
    }
}
